package com.djinfo.work.activiti.dao.impl;

import com.djinfo.work.activiti.dao.RolesDao;
import com.djinfo.work.activiti.po.Permission;
import com.djinfo.work.activiti.po.Role_permission;
import com.djinfo.work.common.dao.impl.BaseDaoImpl;
import com.djinfo.work.common.util.StringUtil;
import com.djinfo.work.entity.TbSysRoleUser;
import com.djinfo.work.entity.TbSysRoles;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import java.util.HashSet;
import java.util.List;

/**
 * 请假
 */
@Repository("RolesDao")
public class RolesDaoImpl extends BaseDaoImpl implements RolesDao {


    @Override
    public List<TbSysRoles> getRoleinfo(int pagenum, int pagesize) {
        StringBuffer sql = new StringBuffer();
        sql.append("select * from tb_sys_role r ");
        sql.append("left join role_permission  rp on r.pk_role_id=rp.roleid ");
        sql.append("left join permission p on rp.permissionid=p.id ");
        sql.append("WHERE 1=1");
        SQLQuery sQLQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql.toString());
        sQLQuery.addEntity("a", TbSysRoles.class);
        sQLQuery.setFirstResult((pagenum - 1) * pagesize).setMaxResults(
                pagesize);
        List<TbSysRoles> tbSysRolesList = sQLQuery.list();
        removeDuplicate(tbSysRolesList);
        return tbSysRolesList;
    }

    public  void removeDuplicate(List arlList)
    {
        HashSet h = new HashSet(arlList);
        arlList.clear();
        arlList.addAll(h);
    }

    @Override
    public List<TbSysRoles> getallRoleinfo() {
        StringBuffer sql = new StringBuffer();
        sql.append("select * from tb_sys_role r ");
        sql.append("left join role_permission  rp on r.pk_role_id=rp.roleid ");
        sql.append("left join permission p on rp.permissionid=p.id ");
        sql.append("WHERE 1=1");
        SQLQuery sQLQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql.toString());
        sQLQuery.addEntity("a", TbSysRoles.class);
        List<TbSysRoles> tbSysRolesList = sQLQuery.list();
        return tbSysRolesList;
    }

    @Override
    public TbSysRoles getTbSysRolesbyid(String id) {
        StringBuffer sql = new StringBuffer();
        sql.append("select r.* from tb_sys_role r ");
        sql.append("left join role_permission rp on r.pk_role_id=rp.roleid ");
        sql.append("left join permission p on rp.permissionid=p.id ");
        sql.append("WHERE r.pk_role_id= '"+id+"' and 1=1");
        SQLQuery sQLQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql.toString());
        sQLQuery.addEntity("a", TbSysRoles.class);
        List<TbSysRoles> tbSysRolesList = sQLQuery.list();
        TbSysRoles tbSysRoles =null;
        List<Role_permission> role_permissionList =null;
        if (tbSysRolesList.size()>0){
            tbSysRoles =tbSysRolesList.get(0);
            role_permissionList = this.findByProperty(Role_permission.class,"roleid",tbSysRoles.getPkRoleId());
            for(Role_permission role_permission:role_permissionList){
                Permission permission =null;
                String permissionid = role_permission.getPermissionid();
                permission = this.findUniqueByProperty(Permission.class,"pid",permissionid);
                role_permission.setPermission(permission);
            }
        }
        tbSysRoles.setRole_permissions(role_permissionList);
        return tbSysRoles;
    }

    @Override
    public List<TbSysRoleUser> getTbSysRoleUser(String id) {
        List<TbSysRoleUser> tbSysRoleUserList = this.findByProperty(TbSysRoleUser.class,"userId",id);
        return tbSysRoleUserList;
    }

    @Override
    public void deleterole(String rid) {
        TbSysRoleUser tbSysRoleUser = this.findUniqueByProperty(TbSysRoleUser.class,"roleId",rid);
        if(StringUtil.isNotEmpty(tbSysRoleUser)){
            this.delete(tbSysRoleUser);
        }
    }

    @Override
    public void deleterolepermission(String rid) {
        String sql ="delete from role_permission where roleid='"+rid+"'";
        SQLQuery sQLQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql.toString());
        sQLQuery.executeUpdate();
    }

}
